using System;
using System.Collections;
using System.Text;
using System.Data;

namespace NET.Library.Windows.Forms.Controls
{
    /// <summary>
    /// ComboBox Object
    /// </summary>
    public static class ComboBox
    {
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Populate a ComboBox's DataSource with an IList created from a specified DataSet.
        /// </summary>
        /// <param name="DataSet"></param>
        /// <returns>DataTable</returns>
        ///----------------------------------------------------------------------------------
        public static DataTable SetDataSource(DataSet DataSet)
        {
            //IList li = new ArrayList();
            DataTable dt = new DataTable();

            foreach (DataColumn dc in DataSet.Tables[0].Columns)
            {
                dt.Columns.Add(dc.ColumnName);
            }

            dt.Rows.Add(new Object[] { 0, "Select..." });

            //He we'll always assume the first colum is the value and the second column is the text.
            string valueMember = dt.Columns[0].ColumnName;
            string displayMember = dt.Columns[1].ColumnName;

            foreach (DataRow dr in DataSet.Tables[0].Rows)
            {
                //li.Add(dr);
                dt.Rows.Add(new Object[] { dr[valueMember], dr[displayMember] });
            }

            return dt;
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Populate a ComboBox's DataSource with a DataTable created from a specified DataSet 
        /// and set the Value and Display items of the ComboBox from the specified items.
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="valueMember"></param>
        /// <param name="displayMember"></param>
        /// <returns>DataTable</returns>
        ///----------------------------------------------------------------------------------
        public static DataTable SetDataSource(DataSet dataSet, string valueMember, string displayMember)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add(valueMember);
            dt.Columns.Add(displayMember);

            dt.Rows.Add(new Object[] { 0, "Select..." });

            foreach (DataRow dr in dataSet.Tables[0].Rows)
            {
                dt.Rows.Add(new Object[] { dr[valueMember], dr[displayMember] });
            }
            return dt;
        }

        //Added 20091125
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Populate a ComboBox's DataSource with a DataTable from a apecified DataSet
        /// and set the Value and Display items of the ComboBox from the specified items in the DataTable.
        /// With this method you can also specifiy the top "Select..." text that you want to use.
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="valueMember"></param>
        /// <param name="displayMember"></param>
        /// <param name="selectText"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static DataTable SetDataSource(DataSet dataSet, string valueMember, string displayMember, string selectText)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add(valueMember);
            dt.Columns.Add(displayMember);

            dt.Rows.Add(new Object[] { 0, selectText });

            foreach (DataRow dr in dataSet.Tables[0].Rows)
            {
                dt.Rows.Add(new Object[] { dr[valueMember], dr[displayMember] });
            }
            return dt;
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Matches the given text to text in a ComboBox's list and selects that item.
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="textToSelect"></param>
        ///----------------------------------------------------------------------------------
        public static void SetSelectedText(System.Windows.Forms.ComboBox comboBox, string textToSelect)
        {
            if (textToSelect.Length > 0)
            {
                for(int i=0;i<comboBox.Items.Count;i++)
                {
                    string value = comboBox.GetItemText(comboBox.Items[i]);
                    if (value == textToSelect)
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }
}
